﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using TankServer.Core;

namespace TankServer.Logic
{
    public class HandlePlayerMsg
    {
        public void MsgGetScore(Player player,ProtocolBase protoBase)
        {
            ProtocolBytes protocolRet = new ProtocolBytes();
            protocolRet.AddString("GetScore");
            protocolRet.AddInt(player.data.score);
            player.Send(protocolRet);
            Console.WriteLine("MsgGetScore"+player.id+player.data.score);

        }

        public void MsgAddScore(Player player,ProtocolBase protoBase)
        {
            int start = 0;
            ProtocolBytes protocol = (ProtocolBytes)protoBase;
            string protoName = protocol.GetString(start,ref start);
            player.data.score += 1;
            Console.WriteLine("MsgAddScore "+player.id+" "+player.data.score.ToString());
        }

        public void MsgGetList(Player player,ProtocolBase protoBase)
        {
            Scene.instance.SendPlayerList(player);
        }
       
        public void MsgUpdateInfo(Player player,ProtocolBase protoBase)
        {
            int start = 0;
            ProtocolBytes protocol = (ProtocolBytes)protoBase;
            string protoName = protocol.GetString(start,ref start);
            float x = protocol.GetFloat(start,ref start);
            float y = protocol.GetFloat(start,ref start);
            float z = protocol.GetFloat(start,ref start);
            int score = player.data.score;
            Scene.instance.UpdateInfo(player.id,x,y,z,score);
            ProtocolBytes protocolRet = new ProtocolBytes();
            protocolRet.AddString("UpdateInfo");
            protocolRet.AddString(player.id);
            protocolRet.AddFloat(x);
            protocolRet.AddFloat(y);
            protocolRet.AddFloat(z);
            protocolRet.AddFloat(score);
            ServNet.instance.Broadcast(protocolRet);
        }

        //获取玩家信息
        public void MsgGetArchieve(Player player,ProtocolBase protoBase)
        {
            ProtocolBytes protocolRet = new ProtocolBytes();
            protocolRet.AddString("GetArchieve");
            protocolRet.AddInt(player.data.win);
            protocolRet.AddInt(player.data.fail);
            player.Send(protocolRet);
            Console.WriteLine("MsgGetArchieve"+player.id+player.data.win);
        }

        public void MsgGetRoomList(Player player,ProtocolBase protoBase)
        {
            player.Send(RoomMgr.instance.GetRoomList());
        }
        //创建房间
        public void MsgCreateRoom(Player player,ProtocolBase protoBase)
        {
            ProtocolBytes protocol = new ProtocolBytes();
            protocol.AddString("CreateRoom");
            if (player.tempData.status != PlayerTempData.Status.None)
            {
                Console.WriteLine("MsgCreateRoom Fail "+player.id);
                protocol.AddInt(-1);
                player.Send(protocol);
                return;
            }
            RoomMgr.instance.CreateRoom(player);
            protocol.AddInt(0);
            player.Send(protocol);
            Console.WriteLine("MsgCreateRoom OK"+player.id);
        }
        //加入房间
        public void MsgEnterRoom(Player player,ProtocolBase protoBase)
        {
            int start = 0;
            ProtocolBytes protocol = (ProtocolBytes)protoBase;
            string protoName = protocol.GetString(start,ref start);
            int index = protocol.GetInt(start,ref start);
            Console.WriteLine("[收到MsgEnterRoom]"+player.id+" "+index);
            protocol = new ProtocolBytes();
            protocol.AddString("EnterRoom");
            if (index < 0 || index >= RoomMgr.instance.list.Count)
            {
                Console.WriteLine("MsgEnterRoom index error "+player.id);
                protocol.AddInt(-1);
                player.Send(protocol);
                return;
            }
            Room room = RoomMgr.instance.list[index];
            if(room.status != Room.Status.Prepare)
            {
                Console.WriteLine("MsgEnterRoom status error "+player.id);
                protocol.AddInt(-1);
                player.Send(protocol);
                return;
            }
            //添加
            if (room.AddPlayer(player))
            {
                room.Broadcast(room.GetRoomInfo());
                protocol.AddInt(0);
                player.Send(protocol);
            }
            else
            {
                Console.WriteLine("MsgEnterRoom MaxPlayer err "+player.id);
                protocol.AddInt(-1);
                player.Send(protocol);
            }
        }

        public void MsgGetRoomInfo(Player player,ProtocolBase protoBase)
        {
            if (player.tempData.status != PlayerTempData.Status.Room)
            {
                Console.WriteLine("MsgGetRoomInfo status err "+player.id);
                return;
            }
            Room room = player.tempData.room;
            player.Send(room.GetRoomInfo());
        }

        //离开房间
        public void MsgLeaveRoom(Player player,ProtocolBase protoBase)
        {
            ProtocolBytes protocol = new ProtocolBytes();
            protocol.AddString("LeaveRoom");
            if(player.tempData.status != PlayerTempData.Status.Room)
            {
                Console.WriteLine("MsgLeaveRoom status err "+player.id);
                protocol.AddInt(-1);
                player.Send(protocol);
                return;
            }
            Room room = player.tempData.room;
            RoomMgr.instance.LeaveRoom(player);
            protocol.AddInt(0);
            player.Send(protocol);
            if(room != null)
            {
                room.Broadcast(room.GetRoomInfo());
            }
        }


        public void MsgStartFight(Player player,ProtocolBase protoBase)
        {
            ProtocolBytes protocol = new ProtocolBytes();
            protocol.AddString("StartFight");
            //条件判断
            if(player.tempData.status != PlayerTempData.Status.Room)
            {
                Console.WriteLine("MsgStartFight status err" + player.id);
                protocol.AddInt(-1);
                player.Send(protocol);
                return;
            }

            if (!player.tempData.isOwner)
            {
                Console.WriteLine("MsgStartFight owner err "+player.id);
                protocol.AddInt(-1);
                player.Send(protocol);
                return;
            }

            Room room = player.tempData.room;
            if (!room.CanStart())
            {
                Console.WriteLine("MsgStartFight canStart err"+player.id);
                protocol.AddInt(-1);
                player.Send(protocol);
                return;
            }

            protocol.AddInt(0);
            player.Send(protocol);

            room.StartFight();
        }

        public void MsgUpdateUnitInfo(Player player,ProtocolBase protoBase)
        {
            int start = 0;
            ProtocolBytes protocol = (ProtocolBytes)protoBase;
            string protoName = protocol.GetString(start,ref start);
            float posX = protocol.GetFloat(start,ref start);
            float posY = protocol.GetFloat(start, ref start);
            float posZ = protocol.GetFloat(start, ref start);
            float rotX = protocol.GetFloat(start, ref start);
            float rotY = protocol.GetFloat(start, ref start);
            float rotZ = protocol.GetFloat(start, ref start);
            float gunRot = protocol.GetFloat(start, ref start);
            float gunRoll = protocol.GetFloat(start,ref start);
            if(player.tempData.status != PlayerTempData.Status.Fight)
            {
                return;
            }
            Room room = player.tempData.room;
            player.tempData.posX = posX;
            player.tempData.posY = posY;
            player.tempData.posZ = posZ;
            player.tempData.lastUpdateTime = Sys.GetTimeStamp();

            ProtocolBytes protocolRet = new ProtocolBytes();
            protocolRet.AddString("UpdateUnitInfo");
            protocolRet.AddString(player.id);
            protocolRet.AddFloat(posX);
            protocolRet.AddFloat(posY);
            protocolRet.AddFloat(posZ);
            protocolRet.AddFloat(rotX);
            protocolRet.AddFloat(rotY);
            protocolRet.AddFloat(rotZ);
            protocolRet.AddFloat(gunRot);
            protocolRet.AddFloat(gunRoll);
            room.Broadcast(protocolRet);
        }

        public void MsgShooting(Player player, ProtocolBase protoBase)
        {
            int start = 0;
            ProtocolBytes protocol = (ProtocolBytes)protoBase;
            string protoName = protocol.GetString(start, ref start);
            float posX = protocol.GetFloat(start, ref start);
            float posY = protocol.GetFloat(start, ref start);
            float posZ = protocol.GetFloat(start, ref start);
            float rotX = protocol.GetFloat(start, ref start);
            float rotY = protocol.GetFloat(start, ref start);
            float rotZ = protocol.GetFloat(start, ref start);

            if (player.tempData.status != PlayerTempData.Status.Fight)
            {
                return;
            }
            Room room = player.tempData.room;
            ProtocolBytes protocolRet = new ProtocolBytes();
            protocolRet.AddString("Shooting");
            protocolRet.AddString(player.id);
            protocolRet.AddFloat(posX);
            protocolRet.AddFloat(posY);
            protocolRet.AddFloat(posZ);
            protocolRet.AddFloat(rotX);
            protocolRet.AddFloat(rotY);
            protocolRet.AddFloat(rotZ);
            room.Broadcast(protocolRet);
        }

        public void MsgHit(Player player,ProtocolBase protoBase)
        {
            int start = 0;
            ProtocolBytes protocol = (ProtocolBytes)protoBase;
            string protoName = protocol.GetString(start,ref start);
            string enemyName = protocol.GetString(start,ref start);
            float damage = protocol.GetFloat(start,ref start);

            long lastShootTime = player.tempData.lastShootTime;
            if ((Sys.GetTimeStamp() - lastShootTime) < 1)
            {
                Console.WriteLine("MsgHit开炮作弊"+player.id);
                return;
            }
            player.tempData.lastShootTime = Sys.GetTimeStamp();
            if (player.tempData.status != PlayerTempData.Status.Fight)
            {
                return;
            }

            Room room = player.tempData.room;
            if (!room.list.ContainsKey(enemyName))
            {
                Console.WriteLine("MsgHit not Contains enemy"+enemyName);
                return;
            }
            Player enemy = room.list[enemyName];
            if(enemy == null)
            {
                return;
            }
            if(enemy.tempData.hp <= 0)
            {
                return;
            }
            enemy.tempData.hp -= damage;

            ProtocolBytes protocolRet = new ProtocolBytes();
            protocolRet.AddString("Hit");
            protocolRet.AddString(player.id);
            protocolRet.AddString(enemy.id);
            protocolRet.AddFloat(damage);
            room.Broadcast(protocolRet);
            room.UpdateWin();
        }
    }
}
